package com.yc.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oyj.dao.DBHelper;
import com.yc.util.SendMailUtil;

@WebServlet("/member")
public class memberServlet extends BasicServlet {
	
	private DBHelper db = new DBHelper();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/plain;charset=utf-8");
		
		//得到op
		String op = req.getParameter("op");
		
		//开始判断
		if( "sendCode".equals(op) ){
			sendCode(req,resp);
		}else if( "reg".equals(op) ){
			reg(req,resp);
		}else if("login".equals(op)){
			login(req,resp);
		}else if ("getAllTypes".equals(op)){
			getAllTypes(req,resp);
		}else if("checkLogin".equals(op)){
			checkLogin(req,resp);
		}
	}
	
	

	//检查是否登录
	private void checkLogin(HttpServletRequest req, HttpServletResponse resp) {
		Object user = req.getSession().getAttribute("loginUser");
		//判断是否为空
		if( user == null ){
			//没有登录
			this.send(resp, 0);
			return;
		}
		
		//登录了的，那么，我就需要你的姓名
		this.send(resp, user);
	}
	//获取菜单
	private void getAllTypes(HttpServletRequest req, HttpServletResponse resp) {
		String sql = "select * from goodstype";
		this.send(resp, db.findAll(sql));
		
	}

	//登录
	private void login(HttpServletRequest req, HttpServletResponse resp) {
		String account = req.getParameter("account");
		String pwd = req.getParameter("pwd");
		
		String sql = "select * from memberinfo where (nickname = ? or email = ? or tel = ?) and pwd = md5(?)";
		List<Map<String, Object>> list = db.findAll(sql, account,account,account,pwd);
		if( list.size() > 0 ){
			//登录成功
			req.getSession().setAttribute("loginUser", list.get(0));
			this.send(resp, 1);
			return;
		}
		this.send(resp, 0);
	}

	private void reg(HttpServletRequest req, HttpServletResponse resp) {
		String nickname = req.getParameter("nickname");
		String email = req.getParameter("email");
		String pwd = req.getParameter("pwd");
		String yzm = req.getParameter("yzm");
		String tel = req.getParameter("tel");
		
		//通过session，取出存的code
		String saveCode = req.getSession().getAttribute("code") + "";
		//判断
		if( saveCode == null || "".equals(saveCode) || !yzm.equals(saveCode) ){
			this.send(resp, -1);
			return;
		}
		
		//jdbc注册
		try {
			String sql = "insert into memberinfo values(null,?,'',md5(?),?,?,'',now(),1)";
			int result = db.doUpdate(sql, nickname,pwd,tel,email);
			this.send(resp, result);
		} catch (Exception e) {
			this.send(resp, 0);
		}
	}

	//发送验证码
	private void sendCode(HttpServletRequest req, HttpServletResponse resp) {
		//	得到邮箱和姓名
		String name = req.getParameter("name");
		String email = req.getParameter("email");
		
		//生成随机验证码
		String code = "";
		Random r = new Random();
		while ( code.length() < 6 ){
			code += r.nextInt(10);
		}
		
		//开始发送
		SendMailUtil sendMailUtil  =new SendMailUtil();
		if( sendMailUtil.sendQQmail("473404904@qq.com", "pobyeqwcoybtbjbg", email, code, name) ){
			//发送成功
			//存一下code
			HttpSession session = req.getSession();
			session.setAttribute("code", code);
			
			//三分钟之后清除code
			TimerTask tt = new TimerTask(){

				@Override
				public void run() {
					//移除session
					session.removeAttribute("code");	
				}
			} ;
			Timer timer = new Timer();
			timer.schedule(tt, 180000);
			
			this.send(resp, 1);
			return;
		}
		this.send(resp, 0);
	}
	
}
